home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / GLE / BEAM.C next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  2.7 KB  |  119 lines

  1. /* 
  2.  * beam.c
  3.  *
  4.  * FUNCTION:
  5.  * Show how twisting is applied.
  6.  *
  7.  * HISTORY:
  8.  * -- linas Vepstas October 1991
  9.  * -- heavily modified to draw corrugated surface, Feb 1993, Linas
  10.  * -- modified to demo twistoid March 1993
  11.  * -- port to glut Linas Vepstas March 1995
  12.  */
  13.  
  14. /* required include files */
  15. #include <math.h>
  16. #include <stdlib.h>
  17. #include <GL/glut.h>
  18. #include <GL/tube.h>
  19.  
  20. /* =========================================================== */
  21.  
  22. #define NUM_BEAM_PTS 22 
  23. double beam_spine[NUM_BEAM_PTS][3];
  24. double beam_twists [NUM_BEAM_PTS];
  25.  
  26. #define TSCALE (6.0)
  27.  
  28. #define TPTS(x,y,z) {                \
  29.    beam_spine[i][0] = TSCALE * (x);        \
  30.    beam_spine[i][1] = TSCALE * (y);        \
  31.    beam_spine[i][2] = TSCALE * (z);        \
  32.    i++;                        \
  33. }
  34.  
  35. #define TXZERO() {                \
  36.    beam_twists[i] = 0.0;            \
  37. }
  38. /* =========================================================== */
  39.  
  40. #define SCALE 0.1
  41. #define XSECTION(x,y) {                    \
  42.    double ax, ay, alen;                    \
  43.    xsection[i][0] = SCALE * (x);            \
  44.    xsection[i][1] = SCALE * (y);            \
  45.    if (i!=0) {                        \
  46.       ax = xsection[i][0] - xsection[i-1][0];        \
  47.       ay = xsection[i][1] - xsection[i-1][1];        \
  48.       alen = 1.0 / sqrt (ax*ax + ay*ay);        \
  49.       ax *= alen;   ay *= alen;                \
  50.       xnormal [i-1][0] = - ay;                \
  51.       xnormal [i-1][1] = ax;                \
  52.    }                            \
  53.    i++;                            \
  54. }
  55.  
  56. #define NUM_XSECTION_PTS (12)
  57.  
  58. double xsection [NUM_XSECTION_PTS][2];
  59. double xnormal [NUM_XSECTION_PTS][2];
  60.  
  61. /* =========================================================== */
  62.  
  63. void InitStuff (void)
  64. {
  65.    int i;
  66.  
  67.    i=0;
  68.    while (i<22) {
  69.       TXZERO ();
  70.       TPTS (-1.1 +((float) i)/10.0, 0.0, 0.0);
  71.    }
  72.  
  73.    i=0;
  74.    XSECTION (-6.0, 6.0);
  75.    XSECTION (6.0, 6.0);
  76.    XSECTION (6.0, 5.0);
  77.    XSECTION (1.0, 5.0);
  78.    XSECTION (1.0, -5.0);
  79.    XSECTION (6.0, -5.0);
  80.    XSECTION (6.0, -6.0);
  81.    XSECTION (-6.0, -6.0);
  82.    XSECTION (-6.0, -5.0);
  83.    XSECTION (-1.0, -5.0);
  84.    XSECTION (-1.0, 5.0);
  85.    XSECTION (-6.0, 5.0);
  86. }
  87.  
  88. void TwistBeam (double howmuch) {
  89.  
  90.    int i;
  91.    double z;
  92.    for (i=0; i<22; i++) {
  93.       z = ((double) (i-14)) / 10.0;
  94.       beam_twists[i] = howmuch * exp (-3.0 * z*z);
  95.    }
  96. }
  97.  
  98. /* =========================================================== */
  99.  
  100. extern float lastx;
  101.  
  102. void DrawStuff (void) {
  103.    TwistBeam ((double) (lastx -121) / 8.0);
  104.  
  105.    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  106.  
  107.    /* set up some matrices so that the object spins with the mouse */
  108.    glPushMatrix ();
  109.    glTranslatef (0.0, 0.0, -80.0);
  110.    glRotated (43.0, 1.0, 0.0, 0.0);
  111.    glRotated (43.0, 0.0, 1.0, 0.0);
  112.    glScaled (1.8, 1.8, 1.8);
  113.    gleTwistExtrusion (NUM_XSECTION_PTS, xsection, xnormal, 
  114.               NULL, NUM_BEAM_PTS, beam_spine, NULL, beam_twists);
  115.    glPopMatrix ();
  116.    glutSwapBuffers ();
  117. }
  118. /* ------------------ end of file -------------------- */
  119.